package io.github.liuyuhao412.manager.mapper;

import java.util.List;

import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Options;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import io.github.liuyuhao412.manager.entity.User;

@Mapper
public interface UserMapper {

	// 登录
	@Select("SELECT * FROM user WHERE username = #{username} AND password = #{password}")
	User login(@Param("username") String username, @Param("password") String password);

	// 查询用户列表
	@Select("SELECT id, username, email, phone, status, role, created_at, updated_at FROM user")
	List<User> listUsers();

	@Insert("INSERT INTO user(username, password, role, status) VALUES(#{username}, #{password}, #{role}, #{status})")
	@Options(useGeneratedKeys = true, keyProperty = "id")
	int insertUser(User user);

	// 更新用户（包括角色）
	@Update("UPDATE user SET username = #{username},  role = #{role}, status = #{status}, updated_at = CURRENT_TIMESTAMP WHERE id = #{id}")
	int updateUser(User user);

	// 删除用户
	@Delete("DELETE FROM user WHERE id = #{id}")
	int deleteUser(Integer id);

	// 根据 id 查询用户
	@Select("SELECT * FROM user WHERE id = #{id}")
	User getUserById(@Param("id") Integer id);

	// 根据用户名查询用户
	@Select("SELECT * FROM user WHERE username = #{username}")
	User getUserByUsername(@Param("username") String username);
}
